package com.ruoyi.system.interAttr.attr;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.outto.core.MapUntils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 密码游戏列表没有接口
 *
 * 等对面开发人员写好   2024 06 27
 *
 */
@Slf4j
@Service
public class DD373Attr implements AdapterAttr {


    public static void main(String[] args) {
//        /Api/OpenApi/GoodsGetPublishInfo
//        String response = HttpRequest.get("https://game.dd373.com/Api/GameOther/List?parentId=8ee0515e-adda-47e7-980d-415fc1fe5c54").execute().body();
//        System.out.println(response);
    }

    @Override
    public List<GamesRes> getGames(SalePartner salePartner) {
        String result = HttpRequest.get("https://game.dd373.com/api/game/list").execute().body();
        String statusData = JSONObject.parseObject(result).getString("StatusData");
        String ResultData = JSONObject.parseObject(statusData).getString("ResultData");
        List<JSONObject> jsonObjects = JSONObject.parseArray(ResultData, JSONObject.class);
        List<GamesRes> gamesResList = new ArrayList<>();

        for (JSONObject jsonObject : jsonObjects) {
            GamesRes gamesRes = new GamesRes();
            String id = jsonObject.getString("Id");
            String Name = jsonObject.getString("Name");
            gamesRes.setId(id+"_"+Name);
            gamesRes.setGameName(Name);
            gamesResList.add(gamesRes);
        }
        return gamesResList;
    }

    @Override
    public List<InterServerRes> getServer(String gameId, SalePartner salePartner) {
        String response = HttpRequest.get("https://game.dd373.com/Api/GameOther/List?parentId="+gameId.split("_")[0]).execute().body();
        String StatusData = JSONObject.parseObject(response).getString("StatusData");
        String ResultData = JSONObject.parseObject(StatusData).getString("ResultData");
        String GameOther = JSONObject.parseObject(ResultData).getString("GameOther");
        List<JSONObject> data = JSONObject.parseArray(GameOther, JSONObject.class);

        List<InterServerRes> interServerRes = new ArrayList<>();
        for (JSONObject datum : data) {
            InterServerRes interServer = new InterServerRes();
            interServer.setId(datum.getString("Id"));
            interServer.setName(datum.getString("Name"));
            String response2 = HttpRequest.get("https://game.dd373.com/Api/GameOther/List?parentId="+datum.getString("Id")).execute().body();
            if(StringUtils.isNotEmpty(response2)) {
                List<InterServerRes> interServerRes1 = new ArrayList<>();
                String StatusData2 = JSONObject.parseObject(response2).getString("StatusData");
                String ResultData2 = JSONObject.parseObject(StatusData2).getString("ResultData");
                String GameOther2 = JSONObject.parseObject(ResultData2).getString("GameOther");
                List<JSONObject> data2 = JSONObject.parseArray(GameOther2, JSONObject.class);
                for (JSONObject object : data2) {
                    InterServerRes interServer1 = new InterServerRes();
                    String id1 = object.getString("Id");
                    String title1 = object.getString("Name");
                    interServer1.setId(id1);
                    interServer1.setName(title1);
                    String response3 = HttpRequest.get("https://game.dd373.com/Api/GameOther/List?parentId="+id1).execute().body();
                    if(StringUtils.isNotEmpty(response3)){
                        List<InterServerRes> interServerRes3 = new ArrayList<>();
                        String StatusData3 = JSONObject.parseObject(response3).getString("StatusData");
                        String ResultData3 = JSONObject.parseObject(StatusData3).getString("ResultData");
                        String GameOther3 = JSONObject.parseObject(ResultData3).getString("GameOther");
                        List<JSONObject> data3 = JSONObject.parseArray(GameOther3, JSONObject.class);
                        for (JSONObject object3 : data3) {
                            InterServerRes interServer3 = new InterServerRes();
                            String id3 = object3.getString("Id");
                            String title3 = object3.getString("Name");
                            interServer3.setId(id3);
                            interServer3.setName(title3);
                            interServerRes3.add(interServer3);
                        }
                        interServer1.setInterServerRes(interServerRes3);
                    }
                    interServerRes1.add(interServer1);
                }
                interServer.setInterServerRes(interServerRes1);
            }
            interServerRes.add(interServer);
        }
        return interServerRes;
    }

    @Override
    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        Map<String,Object> params = new TreeMap<>();
        Map<String,Object> jsonData = new TreeMap<>();
        jsonData.put("GameInfo",gameId.split("_")[1]);
        jsonData.put("GoodsTypeName","游戏账号");
        params.put("AppKey","TlT1Mbo36HOC5phW0tE61KKBayl8ySzF");
        params.put("JsonData",JSONObject.toJSONString(jsonData));
        params.put("Method","goods.getpublishinfo");
        params.put("Timestamp",System.currentTimeMillis()/1000);
        String cover = MapUntils.getCover(params);
        cover = cover.substring(0,cover.length()-1)+"n9bVd6jp1BhjJvk7mqxB3nu2nvgMtO6k";
        System.out.println(cover);
        String Sign = DigestUtils.md5Hex(cover);
        params.put("Sign",Sign);
        String body = HttpRequest.post("https://goods.dd373.com/Api/Open/Third").body(JSONObject.toJSONString(params)).execute().body();
        String resultData = JSONObject.parseObject(body).getString("ResultData");
        String Attrubutes = JSONObject.parseObject(resultData).getString("Attrubutes");
        List<JSONObject> data = JSONObject.parseArray(Attrubutes, JSONObject.class);
        List<JSONObject> collect = new ArrayList<>();
        List<InterAttrRes> interAttrResList = new ArrayList<>();
        if (Objects.equals("1",type)){
            collect = data.stream().filter(d -> d.getString("FormType").equals("0")).collect(Collectors.toList());
        }else if  (Objects.equals("2",type)){
            collect = data.stream().filter(d -> d.getString("FormType").equals("2")).collect(Collectors.toList());
        }else if (Objects.equals("3",type)){
            collect = data.stream().filter(d -> d.getString("FormType").equals("1")).collect(Collectors.toList());
        }
        if(CollectionUtils.isNotEmpty(collect)) {
            for (JSONObject object : collect) {
                InterAttrRes interAttrRes = new InterAttrRes();
                interAttrRes.setId(object.getString("TypeId"));
                interAttrRes.setName(object.getString("TypeName"));
                List<InterAttrRes> interAttrResList1 = new ArrayList<>();
                String AttributeValueList = object.getString("AttributeValueList");
                if (StringUtils.isNotEmpty(AttributeValueList)) {
                    JSONArray jsonArray = JSONArray.parseArray(AttributeValueList);
                    for (Object o : jsonArray) {
                        InterAttrRes interAttrRes1 = new InterAttrRes();
                        interAttrRes1.setId(JSONObject.parseObject(o.toString()).getString("ValueId"));
                        interAttrRes1.setName(JSONObject.parseObject(o.toString()).getString("ValueName"));
                        interAttrResList1.add(interAttrRes1);
                        interAttrRes.setInterServerRes(interAttrResList1);
                    }
                }
                interAttrResList.add(interAttrRes);
            }
        }else {
            InterAttrRes interAttrRes = new InterAttrRes();
            interAttrRes.setId("该游戏没配置属性");
            interAttrRes.setName("该游戏没配置属性");
            interAttrResList.add(interAttrRes);
        }
        return interAttrResList;
    }
}